package testing;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.concurrent.CountDownLatch;

import javax.swing.JFrame;

import net.java.games.input.Component;
import net.java.games.input.Controller;
import net.java.games.input.Controller.Type;
import net.java.games.input.ControllerEnvironment;

import logic.CircleObject;
import logic.Scene;
import logic.SceneListener;
import logic.Vec2D;
import logic.server.Player;

import util.Util;
import vis.ViewPanel;

public class TestFrame extends JFrame implements KeyListener, SceneListener {
	private static final long serialVersionUID = -7500210243294265892L;
	private Scene scene;
	
	private float forceAmmount = 2.0f;
	private float sprintForce = 4.0f;
	private int score1 = 0, score2 = 0;
	private static final float DEAD_ZONE = 0.8f; 
	
	private int resetCountdown = 0;
	
	public TestFrame()
	{
		super("Hax0r'z Ballz");
		
		scene = new Scene();
		scene.setSceneListener(this);
		
		
		ViewPanel view = new ViewPanel(scene);
		add(view);
		
		//setSize(640, 360);
		pack();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		//setResizable(false);
		setMaximumSize(getSize());
		setMinimumSize(getSize());
		
		Thread t = new Thread(new Runnable() {
			
			@Override
			public void run() {
				while(true)
				{
					if(joystick != null)
					{
						joystick.poll();
						//System.out.println("X : " + Xaxis.getPollData());					
						//System.out.println("Y : " + Yaxis.getPollData());
						//System.out.println("A : " + Abutton.getPollData());
						
						kW2 = Yaxis.getPollData() < -DEAD_ZONE;
						kS2 = Yaxis.getPollData() > DEAD_ZONE;
						kA2 = Xaxis.getPollData() < -DEAD_ZONE;
						kD2 = Xaxis.getPollData() > DEAD_ZONE;
						if(scene.getP2().getSprint() > Player.SPRINT_MAX / 2)
							kSprint2 = Abutton.getPollData() > DEAD_ZONE;
						else
							if(Abutton.getPollData() <= DEAD_ZONE)
								kSprint2 = false;
						kShoot2 = Bbutton.getPollData() > DEAD_ZONE;
					}
					
					Vec2D f1 = new Vec2D(0, 0);
					if(kW1) f1 = f1.add(0.0f, -2.0f);
					if(kS1) f1 = f1.add(0.0f, 2.0f);
					if(kA1) f1 = f1.add(-2.0f, 0.0f);
					if(kD1) f1 = f1.add(2.0f, 0.0f);
					if(kShoot1 && scene.getP1().isCanShoot())
						scene.getP1().shoot();
					
					f1.normalize();
					if(scene.getP1().getSprint() > 0 && kSprint1)
					{
						f1 = f1.mul(sprintForce);
						scene.getP1().subSprint();
					}
					else
					{
						f1 = f1.mul(forceAmmount);
					}
					scene.getP1().applyForce(f1);
					
					Vec2D f2 = new Vec2D(0, 0);
					if(kW2) f2 = f2.add(0.0f, -2.0f);
					if(kS2) f2 = f2.add(0.0f, 2.0f);
					if(kA2) f2 = f2.add(-2.0f, 0.0f);
					if(kD2) f2 = f2.add(2.0f, 0.0f);
					if(kShoot2 && scene.getP2().isCanShoot())
						scene.getP2().shoot();
					
					f2.normalize();
					if(scene.getP2().getSprint() > 0 && kSprint2)
					{
						f2 = f2.mul(sprintForce);
						scene.getP2().subSprint();
					}
					else
					{
						f2 = f2.mul(forceAmmount);
					}
					scene.getP2().applyForce(f2);
					
					scene.update();
					repaint();
					try {
						Thread.sleep(20);
					} catch (InterruptedException e) {}
				}
				
			}
		});
		
		initJoystick();
		addKeyListener(this);
		t.start();
	}
	
	private Controller joystick = null;
	private Component Xaxis = null;
	private Component Yaxis = null;
	private Component Abutton = null;
	private Component Bbutton = null;
	
	private void initJoystick()
	{
	
	  ControllerEnvironment ce = 
	         ControllerEnvironment.getDefaultEnvironment(); 
	  Controller[] cs = ce.getControllers(); 
	  // print the name and type of each controller 
	  for (int i = 0; i < cs.length; i++)
	  {
	    //System.out.println(i + ". " + 
	      //     cs[i].getName() + ", " + cs[i].getType() );
	    if(cs[i].getType() == Type.GAMEPAD)
	    {
	    	joystick = cs[i];
	    	//System.out.println("Gamepad found!");
	    	//System.out.println(joystick.getComponents());
	    	//for(Component c : joystick.getComponents())
	    		//System.out.println(c);
	    	
	    	Xaxis = joystick.getComponents()[4];
	    	Yaxis = joystick.getComponents()[3];
	    	Abutton = joystick.getComponents()[8];
	    	Bbutton = joystick.getComponents()[9];
	    }
	  }
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new TestFrame();
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	boolean kW1, kS1, kA1, kD1, kSprint1, kShoot1;
	boolean kW2, kS2, kA2, kD2, kSprint2, kShoot2;
	
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyChar() == 'w')
		{
			kW1 = true;
		}
		else if(e.getKeyChar() == 's')
		{
			kS1 = true;
		}
		else if(e.getKeyChar() == 'a')
		{
			kA1 = true;
		}
		else if(e.getKeyChar() == 'd')
		{
			kD1 = true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_CONTROL)
		{
			if(scene.getP1().getSprint() > Player.SPRINT_MAX / 2)
				kSprint1 = true;
		}
		else if(e.getKeyCode() == 16)
		{
			kShoot1 = true;
		}
		
		if(e.getKeyCode() == KeyEvent.VK_NUMPAD8)
		{
			kW2 = true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_NUMPAD5)
		{
			kS2 = true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_NUMPAD4)
		{
			kA2 = true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_NUMPAD6)
		{
			kD2 = true;
		}
		else if(e.getKeyCode() == KeyEvent.VK_NUMPAD0)
		{
			if(scene.getP2().getSprint() > Player.SPRINT_MAX / 2)
				kSprint2 = true;
		}
		else if(e.getKeyCode() == 110)
		{
			kShoot2 = true;
		}
	}
	@Override
	public void keyReleased(KeyEvent e) {
		if(e.getKeyChar() == 'w')
		{
			kW1 = false;
		}
		else if(e.getKeyChar() == 's')
		{
			kS1 = false;
		}
		else if(e.getKeyChar() == 'a')
		{
			kA1 = false;
		}
		else if(e.getKeyChar() == 'd')
		{
			kD1 = false;
		}
		else if(e.getKeyCode() == KeyEvent.VK_CONTROL)
		{
			kSprint1 = false;
		}
		else if(e.getKeyCode() == 16)
		{
			kShoot1 = false;
		}
		
		
		if(e.getKeyCode() == KeyEvent.VK_NUMPAD8)
		{
			kW2 = false;
		}
		else if(e.getKeyCode() == KeyEvent.VK_NUMPAD5)
		{
			kS2 = false;
		}
		else if(e.getKeyCode() == KeyEvent.VK_NUMPAD4)
		{
			kA2 = false;
		}
		else if(e.getKeyCode() == KeyEvent.VK_NUMPAD6)
		{
			kD2 = false;
		}
		else if(e.getKeyCode() == KeyEvent.VK_NUMPAD0)
		{
			kSprint2 = false;
		}
		else if(e.getKeyCode() == 110)
		{
			kShoot2 = false;
		}
		//System.out.println(e.getKeyCode());
	}
	@Override
	public void goal(int side) {
		resetCountdown = 200;
		if(side == 0)
			score2++;
		else
			score1++;
	}

}
